package com.wh.weibo.controller;

import com.wh.weibo.model.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WeiHong
 * @date 2019/7/8 19:10
 */
public class BaseController {

    @Resource
    private HttpServletRequest httpServletRequest;

    protected User getCurrentUser() {
        User currentUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
        return currentUser;
    }

    /**
     * 返回成功，统一出口
     *
     * @param data
     * @return
     */
    protected Map getResp(Object data) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", true);
        result.put("data", data);
        return result;
    }

    /**
     * 返回失败，统一出口
     *
     * @param errorMsg
     * @return
     */
    protected Map getErrorResp(String errorMsg) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", false);
        result.put("data", errorMsg);
        return result;
    }
}
